cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenMP)

if(OpenMP_FOUND)
  # this will get wiped unless you run cmake with --debug-trycompile
  set(scratch_dir ${CMAKE_CURRENT_BINARY_DIR}/omp_try_compile)

  try_compile(
    omp_task_loop_test_1
    ${scratch_dir}
    SOURCES
      ${CMAKE_CURRENT_SOURCE_DIR}/task_loop.cpp
    LINK_LIBRARIES
      OpenMP::OpenMP_CXX
    )
  message(STATUS "Result of try_compile: ${omp_task_loop_test_1}")

  include(CheckCXXSourceCompiles)

  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/task_loop.cpp snippet)

  set(CMAKE_REQUIRED_LIBRARIES OpenMP::OpenMP_CXX)

  check_cxx_source_compiles("${snippet}" omp_task_loop_test_2)

  unset(CMAKE_REQUIRED_LIBRARIES)

  message(STATUS "Result of check_cxx_source_compiles: ${omp_task_loop_test_2}")
else()
  message(STATUS "OpenMP not found: no test for taskloop is run")
endif()